﻿using System;
using System.Linq;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class MemberList : BaseUserControl
    {
        public String TargetRole { get; set; }
        public String GroupingText { get; set; }

        private static string onlineLocalized = "online".ToLocalizedString();
        private static string awayLocalized = "away".ToLocalizedString();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            MemberListPanel.GroupingText = GroupingText;

            // Afficher la fonction message privé pour les utilisateurs authentifié
            if (ThisMaster.RequesterIsAuthenticated)
            {
                MemberListGridView.Columns[25].Visible = true;
            }

            if (ThisMaster.RequesterRoles.Contains("Member"))
            {
                MemberListGridView.Columns[4].Visible = true;
                MemberListGridView.Columns[5].Visible = true;
                MemberListGridView.Columns[6].Visible = true;
            }

            if (ThisMaster.RequesterRoles.Contains("RoleAdminForum"))
            {
                MemberListGridView.Columns[4].Visible = true;
                MemberListGridView.Columns[5].Visible = true;
                MemberListGridView.Columns[6].Visible = true;
                MemberListGridView.Columns[8].Visible = true;
                MemberListGridView.Columns[14].Visible = true;
                MemberListGridView.Columns[15].Visible = true;
                MemberListGridView.Columns[16].Visible = true;
                MemberListGridView.Columns[17].Visible = true;
                MemberListGridView.Columns[18].Visible = true;
                MemberListGridView.Columns[19].Visible = true;
                MemberListGridView.Columns[20].Visible = true;
                MemberListGridView.Columns[21].Visible = true;
                MemberListGridView.Columns[22].Visible = true;
                MemberListGridView.Columns[23].Visible = true;
                MemberListGridView.Columns[24].Visible = true;
            }
            MemberListGridView.DataSource = (from m in UserProfile.GetUserRoleMembershipArray(TargetRole)
                                             join p in UserProfile.GetUserRoleProfileArray(TargetRole)
                                         on m.UserName equals p.UserName
                                             orderby m.UserName
                                             select new
                                             {
                                                 CreationDate = m.CreationDate,
                                                 GravatarEmail = p.UserGravatarEmail.HtmlToText(),
                                                 Username = m.UserName,
                                                 PseudoBf3 = p.Bf3ColonelNickname.HtmlToText(),
                                                 Culture = p.UserChoosenCulture,
                                                 LastLogin = m.LastLoginDate,
                                                 TpbbBf3c = p.TimePlayedToBecomeBf3Colonel,
                                                 ProfileLastUpdate = p.LastUpdatedDate,
                                                 Status = (m.IsOnline) ? onlineLocalized : awayLocalized,
                                                 NumberOfPost = p.NumberOfForumPost,
                                                 Location = p.Location.HtmlToText(),
                                                 Bf3RankImage = p.Bf3RankImage,
                                                 InitialAddressIp = p.InitialAddressIp,
                                                 LastAddressIp = p.LastAddressIp,
                                                 UserBirthday = ((p.UserBirthday == new DateTime()) ? String.Empty : p.UserBirthday.ToShortDateString()),
                                                 UserTheme = p.UserTheme,
                                                 Bf3BattlelogProfilUrl = p.Bf3BattlelogProfilUrl,
                                                 Comment = m.Comment,
                                                 Email = m.Email,
                                                 IsApproved = m.IsApproved,
                                                 IsLockedOut = m.IsLockedOut,
                                                 LastActivityDate = m.LastActivityDate,
                                                 LastPasswordChangedDate = m.LastPasswordChangedDate,
                                                 PasswordQuestion = m.PasswordQuestion,
                                                 ProviderUserKey = m.ProviderUserKey,
                                                 TimeZone = TimeZoneInfo.FindSystemTimeZoneById(p.TimeZone).DisplayName
                                             });
            MemberListGridView.DataBind();
        }

        protected void MemberListGridView_DataBinding(object sender, EventArgs e)
        {

        }
    }
}